home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / snurb / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.0 KB  |  290 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <gl.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <sys/types.h>
  22. #include <dirent.h>
  23. #include "defines.h"
  24. #include "control.h"
  25. #include "data.h"
  26. #include "file.h"
  27.  
  28. extern struct node_struct *root, *selected_patch;
  29.  
  30.  
  31. static void number_nodes(struct node_struct *object, int *counter)
  32. {
  33.     if (object != NULL)
  34.     {
  35.         object->node_id = *counter;
  36.  
  37.         (*counter)++;
  38.  
  39.         number_nodes(object->child, counter);
  40.         number_nodes(object->sibling, counter);
  41.     }
  42. }
  43.         
  44.  
  45. static write_nodes(struct node_struct *object, FILE *fp)
  46. {
  47.     int i,j,k;
  48.     
  49.     if (object != NULL)
  50.     {
  51.         fprintf(fp, "%d %d\n",object->node_id, object->node_type);
  52.         
  53.         if (object->parent == NULL)
  54.             fprintf(fp, "-1 ");
  55.         else
  56.             fprintf(fp, "%d ", object->parent->node_id);
  57.         
  58.         if (object->child == NULL)
  59.             fprintf(fp, "-1 ");
  60.         else
  61.             fprintf(fp, "%d ", object->child->node_id);
  62.         
  63.         if (object->sibling == NULL)
  64.             fprintf(fp, "-1 ");
  65.         else
  66.             fprintf(fp, "%d ", object->sibling->node_id);
  67.         
  68.         if (object->prev_sibling == NULL)
  69.             fprintf(fp, "-1\n");
  70.         else
  71.             fprintf(fp, "%d\n", object->prev_sibling->node_id);
  72.  
  73.  
  74.         if (object->node_type == PATCH)
  75.         {
  76.             for (i = 0; i< 4; i++)
  77.                 for (j = 0; j < 4; j++)
  78.                     fprintf(fp, "%f %f %f\n", 
  79.                         object->patch->control[i][j][0],
  80.                         object->patch->control[i][j][1],
  81.                         object->patch->control[i][j][2]);
  82.  
  83.             for (i = 0; i<4; i++)
  84.             {
  85.                 fprintf(fp, "%d ", object->patch->zipper[i]->zipped);
  86.                 fprintf(fp, "%d ", object->patch->zipper[i]->zip_type);
  87.                 
  88.                 if (object->patch->zipper[i]->patch == NULL)
  89.                     fprintf(fp, "-1 ");
  90.                 else
  91.                     fprintf(fp, "%d ", object->patch->zipper[i]->patch->node_id);
  92.  
  93.                 fprintf(fp, "%d\n", object->patch->zipper[i]->edge);
  94.  
  95.                 fprintf(fp, "%d %d %d %d\n",
  96.                     object->patch->zipper[i]->s_index,
  97.                     object->patch->zipper[i]->s_step,
  98.                     object->patch->zipper[i]->t_index,
  99.                     object->patch->zipper[i]->t_step);
  100.                     
  101.                 fprintf(fp, "%d %d %d %d\n",
  102.                     object->patch->zipper[i]->i_s_index,
  103.                     object->patch->zipper[i]->i_s_step,
  104.                     object->patch->zipper[i]->i_t_index,
  105.                     object->patch->zipper[i]->i_t_step);
  106.             }                    
  107.         }
  108.         
  109.         fprintf(fp, "\n");
  110.  
  111.         write_nodes(object->child,fp);
  112.         write_nodes(object->sibling,fp);
  113.     }
  114. }
  115.         
  116.  
  117. static void write_file(char *filename, int count)
  118. {
  119.     FILE *fp;
  120.     
  121.     if ( (fp = fopen( filename, "w" )) == NULL )
  122.     {
  123.         fprintf(stderr,"Can't open file for writing\n");
  124.     }
  125.     else
  126.     {
  127.         fprintf(fp,"%d\n",count);
  128.         write_nodes(root, fp);
  129.         fclose(fp);
  130.         
  131.     }
  132. }
  133.  
  134.  
  135. void save_file(void)
  136. {
  137.     int node_count = 0;
  138.     
  139.     number_nodes(root, &node_count);
  140.  
  141.     if (node_count == 1)
  142.         fprintf(stderr, "Nothing to save\n");
  143.     else
  144.         write_file("/tmp/junk.snb", node_count);
  145. }
  146.  
  147. #define MAX_OBJECTS 100
  148. struct node_struct *new_root[MAX_OBJECTS];
  149.  
  150. static void fix(Coord n[3])
  151. {
  152. /*
  153.     if ((n[0] < .1) && (n[0] > -.1))
  154.         n[0] = 0.0;
  155.     else if (n[0] > 0.0)
  156.     {
  157.         if (n[0] <.3)
  158.             n[0] = .276;
  159.         else
  160.             n[0] = .5;
  161.     }
  162.     else
  163.     {
  164.         if (n[0] >-.3)
  165.             n[0] = -.276;
  166.         else
  167.             n[0] = -.5;
  168.     }
  169.     
  170.     if ((n[2] < .1) && (n[2] > -.1))
  171.         n[2] = 0.0;
  172.     else if (n[2] > 0.0)
  173.     {
  174.         if (n[2] <.3)
  175.             n[2] = .276;
  176.         else
  177.             n[2] = .5;
  178.     }
  179.     else
  180.     {
  181.         if (n[2] >-.3)
  182.             n[2] = -.276;
  183.         else
  184.             n[2] = -.5;
  185.     }
  186. */
  187.  
  188.  
  189.  
  190. }
  191.     
  192.         
  193.     
  194.         
  195.         
  196.  
  197. void read_file(char *filename)
  198. {
  199.     FILE *fp;
  200.     int count,i,j,k,a,index;
  201.     struct node_struct *object;
  202.     struct patch_struct *new_patch;
  203.  
  204.     if ( (fp = fopen( filename, "r" )) == NULL )
  205.     {
  206.         fprintf(stderr,"Can't open file for reading\n");
  207.     }
  208.     else
  209.     {
  210.         fscanf(fp,"%d\n",&count);
  211.  
  212.         for (a=0; a<count; a++)
  213.             new_root[a] = make_new_node();
  214.             
  215.         for (a=0; a<count; a++)
  216.         {
  217.             object = new_root[a];
  218.             
  219.             fscanf(fp, "%d %d\n", &object->node_id, &object->node_type);
  220.         
  221.             fscanf(fp, "%d ", &index);
  222.             object->parent = new_root[index];
  223.         
  224.             fscanf(fp, "%d ", &index);
  225.             object->child = new_root[index];
  226.         
  227.             fscanf(fp, "%d ", &index);
  228.             object->sibling = new_root[index];
  229.         
  230.             fscanf(fp, "%d\n", &index);
  231.             object->prev_sibling = new_root[index];
  232.  
  233.             if (object->node_type == PATCH)
  234.             {
  235.                 new_patch = make_new_patch();
  236.                 object->patch = new_patch;
  237.  
  238.                 for (i = 0; i< 4; i++)
  239.                     object->patch->zipper[i] = make_new_zipper();
  240.  
  241.                 
  242.                 for (i = 0; i< 4; i++)
  243.                     for (j = 0; j < 4; j++)
  244.                     {
  245.                         fscanf(fp, "%f %f %f\n", 
  246.                             &object->patch->control[i][j][0],
  247.                             &object->patch->control[i][j][1],
  248.                             &object->patch->control[i][j][2]);
  249.  
  250.                         /* For hacky development use only */
  251.                         fix(object->patch->control[i][j]);
  252.                     }
  253.  
  254.                 for (i = 0; i<4; i++)
  255.                 {
  256.                     fscanf(fp, "%d ", &object->patch->zipper[i]->zipped);
  257.                     fscanf(fp, "%d ", &object->patch->zipper[i]->zip_type);
  258.  
  259.                     fscanf(fp, "%d ", &index);
  260.                     object->patch->zipper[i]->patch = new_root[index];
  261.  
  262.                     fscanf(fp, "%d\n", &object->patch->zipper[i]->edge);
  263.  
  264.                     fscanf(fp, "%d %d %d %d\n",
  265.                         &object->patch->zipper[i]->s_index,
  266.                         &object->patch->zipper[i]->s_step,
  267.                         &object->patch->zipper[i]->t_index,
  268.                         &object->patch->zipper[i]->t_step);
  269.                     
  270.                     fscanf(fp, "%d %d %d %d\n",
  271.                         &object->patch->zipper[i]->i_s_index,
  272.                         &object->patch->zipper[i]->i_s_step,
  273.                         &object->patch->zipper[i]->i_t_index,
  274.                         &object->patch->zipper[i]->i_t_step);
  275.                 }
  276.             }
  277.         }
  278.         fclose(fp);
  279.         new_root[0]->node_type = OBJECT;
  280.         attach_node_to_parent(new_root[0],root);
  281.     }
  282. }
  283.  
  284. void load_file(void)
  285. {
  286.     read_file("/tmp/junk.snb");    
  287.     draw_display();
  288. }
  289.  
  290.